
// Test_C_Log_WriterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test_C_Log_Writer.h"
#include "Test_C_Log_WriterDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTest_C_Log_WriterDlg dialog



CTest_C_Log_WriterDlg::CTest_C_Log_WriterDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CTest_C_Log_WriterDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTest_C_Log_WriterDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_TXT_LOG_FILE_STATE, m_log_file_state);
    DDX_Control(pDX, IDC_TXT_CONSOLE_WINDOW_STATUS, m_console_window_status);
    DDX_Control(pDX, IDC_TXT_WRITE_NULL_POINTER, m_write_null_pointer_to_log);
    DDX_Control(pDX, IDC_TXT_FILE_ENABLE_STATUS, m_file_write_enable_status);
    DDX_Control(pDX, IDC_TXT_CONSOLE_WRITE_ENABLE_STATUS, m_console_write_enable_status);
    DDX_Control(pDX, IDC_TXT_ZERO_LENGTH_WRITE_STATUS, m_zero_length_write_status);
    DDX_Control(pDX, IDC_TXT_WRITE_LEGAL_STRING, m_write_legal_string_count);
    DDX_Control(pDX, IDC_TXT_WRITE_STRING_TOO_LONG, m_write_string_too_long);
    DDX_Control(pDX, IDC_EDIT2, m_user_directory_name);
    DDX_Control(pDX, IDC_TXT_DIRECTORY_SET_STATUS, m_directory_set_status);
    DDX_Control(pDX, IDC_TXT_RE_OPEN_FILE_STATUS, m_reopen_log_file_status);
    DDX_Control(pDX, IDC_TXT_IS_LOG_FILE_OPEN, m_is_log_file_open);
}

BEGIN_MESSAGE_MAP(CTest_C_Log_WriterDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDOK, &CTest_C_Log_WriterDlg::OnBnClickedOk)
    ON_BN_CLICKED(IDC_BTN_OPEN_LOG_FILE, &CTest_C_Log_WriterDlg::OnBnClickedBtnOpenLogFile)
    ON_BN_CLICKED(IDC_BTN_CLOSE_LOG_FILE, &CTest_C_Log_WriterDlg::OnBnClickedBtnCloseLogFile)
    ON_BN_CLICKED(IDC_BTN_OPEN_CONSOLE_WINDOW, &CTest_C_Log_WriterDlg::OnBnClickedBtnOpenConsoleWindow)
    ON_BN_CLICKED(IDC_BTN_CLOSE_CONSOLE_WINDOW, &CTest_C_Log_WriterDlg::OnBnClickedBtnCloseConsoleWindow)
    ON_BN_CLICKED(IDC_BTN_WRITE_NULL_POINTER_TO_LOG, &CTest_C_Log_WriterDlg::OnBnClickedBtnWriteNullPointerToLog)
    ON_BN_CLICKED(IDC_BTN_ENABLE_FILE_WRITES, &CTest_C_Log_WriterDlg::OnBnClickedBtnEnableFileWrites)
    ON_BN_CLICKED(IDC_BTN_DISABLE_FILE_WRITES, &CTest_C_Log_WriterDlg::OnBnClickedBtnDisableFileWrites)
    ON_BN_CLICKED(IDC_BTN_GET_FILE_ENABLE_STATUS, &CTest_C_Log_WriterDlg::OnBnClickedBtnGetFileEnableStatus)
    ON_BN_CLICKED(IDC_BTN_ENABLE_CONSOLE_WRITES, &CTest_C_Log_WriterDlg::OnBnClickedBtnEnableConsoleWrites)
    ON_BN_CLICKED(IDC_BTN_DISABLE_CONSOLE_WRITES, &CTest_C_Log_WriterDlg::OnBnClickedBtnDisableConsoleWrites)
    ON_BN_CLICKED(IDC_BTN_GET_CONSOLE_ENABLE_STATUS, &CTest_C_Log_WriterDlg::OnBnClickedBtnGetConsoleEnableStatus)
    ON_BN_CLICKED(IDC_BTN_WRITE_ZERO_LENGTH_STRING, &CTest_C_Log_WriterDlg::OnBnClickedBtnWriteZeroLengthString)
    ON_BN_CLICKED(IDC_BTN_WRITE_LEGAL_STRING, &CTest_C_Log_WriterDlg::OnBnClickedBtnWriteLegalString)
    ON_BN_CLICKED(IDC_BTN_WRITE_STRING_TOO_LONG, &CTest_C_Log_WriterDlg::OnBnClickedBtnWriteStringTooLong)
    ON_BN_CLICKED(IDC_BTN_SET_DIRECTORY, &CTest_C_Log_WriterDlg::OnBnClickedBtnSetDirectory)
    ON_BN_CLICKED(IDC_BTN_RE_OPEN_LOG_FILE, &CTest_C_Log_WriterDlg::OnBnClickedBtnReOpenLogFile)
    ON_BN_CLICKED(IDC_BTN_IS_LOG_FILE_OPEN, &CTest_C_Log_WriterDlg::OnBnClickedBtnIsLogFileOpen)
END_MESSAGE_MAP()


// CTest_C_Log_WriterDlg message handlers

BOOL CTest_C_Log_WriterDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

    mp_C_Log_Writer = new C_Log_Writer( L"E:\\LOG_FILES", L"Test_Log_Writer" );
    if( mp_C_Log_Writer != NULL &&
        mp_C_Log_Writer != INVALID_HANDLE_VALUE )
    {
        if( mp_C_Log_Writer->Is_Log_File_Open() == true )
            m_log_file_state.SetWindowTextW( L"Open" );
        else
            m_log_file_state.SetWindowTextW( L"Error" );

        if( mp_C_Log_Writer->Is_Console_Window_Open() )
            m_console_window_status.SetWindowTextW( L"Open" );
        else
            m_console_window_status.SetWindowTextW( L"Closed" );

        OnBnClickedBtnGetFileEnableStatus();
        OnBnClickedBtnGetConsoleEnableStatus();
    }

    m_write_null_pointer_counter = 0;
    m_write_zero_length_counter  = 0;
    m_write_legal_counter        = 0;
    m_set_directory_count        = 0;
    m_re_open_log_count          = 0;
    m_is_log_file_open_count     = 0;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTest_C_Log_WriterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTest_C_Log_WriterDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTest_C_Log_WriterDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CTest_C_Log_WriterDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here

    if( mp_C_Log_Writer != NULL )
    {
        delete mp_C_Log_Writer;
        mp_C_Log_Writer = NULL;
    }
    CDialogEx::OnOK();
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnOpenLogFile()
{
    if( mp_C_Log_Writer->Open_Log_File() == true )
    {
        m_log_file_state.SetWindowTextW( L"Open" );
    }
    else
    {
        m_log_file_state.SetWindowTextW( L"Open Error" );
    }
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnReOpenLogFile()
{

    const size_t STRING_SIZE = 30;
    WCHAR local_string[ STRING_SIZE ];
    m_re_open_log_count ++;
    bool status = mp_C_Log_Writer->Re_Open_Log_File( );

    swprintf_s( 
        local_string,
        STRING_SIZE,
        L"%s  %02d",
        status == true ? L"OK    " : L"ERROR ",
        m_re_open_log_count  );

    m_reopen_log_file_status.SetWindowTextW( local_string );

}


void CTest_C_Log_WriterDlg::OnBnClickedBtnIsLogFileOpen()
{
    const size_t STRING_SIZE = 30;
    WCHAR local_string[ STRING_SIZE ];
    m_is_log_file_open_count ++;
    bool status = mp_C_Log_Writer->Is_Log_File_Open( );

    swprintf_s( 
        local_string,
        STRING_SIZE,
        L"%s  %02d",
        status == true ? L"OPEN   " : L"CLOSED ",
        m_is_log_file_open_count  );

    m_is_log_file_open.SetWindowTextW( local_string );
}



void CTest_C_Log_WriterDlg::OnBnClickedBtnCloseLogFile()
{
    if( mp_C_Log_Writer->Close_Log_File() == true )
    {
        m_log_file_state.SetWindowTextW( L"Closed" );
    }
    else
    {
        m_log_file_state.SetWindowTextW( L"Close Error" );
    }
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnOpenConsoleWindow()
{
   if( mp_C_Log_Writer->Open_Console_Window() == true )
    {
        m_console_window_status.SetWindowTextW( L"Open" );
    }
    else
    {
        m_console_window_status.SetWindowTextW( L"Open Error" );
    }
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnCloseConsoleWindow()
{
  if( mp_C_Log_Writer->Close_Console_Window() == true )
    {
        m_console_window_status.SetWindowTextW( L"Closed" );
    }
    else
    {
        m_console_window_status.SetWindowTextW( L"Close Error" );
    }
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnWriteNullPointerToLog()
{
    const size_t STRING_SIZE = 30;
    WCHAR local_string[ STRING_SIZE ];
    m_write_null_pointer_counter ++;
    bool status = mp_C_Log_Writer->Write_Log_File_Entry( NULL );

    swprintf_s( 
        local_string,
        STRING_SIZE,
        L"%s  %02d",
        status == true ? L"OK    " : L"ERROR ",
        m_write_null_pointer_counter  );

    m_write_null_pointer_to_log.SetWindowTextW( local_string );

}


void CTest_C_Log_WriterDlg::OnBnClickedBtnEnableFileWrites()
{
    mp_C_Log_Writer->Set_Log_File_Write_Enable_Flag( ENABLE_WRITES );
    OnBnClickedBtnGetFileEnableStatus();
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnDisableFileWrites()
{
    mp_C_Log_Writer->Set_Log_File_Write_Enable_Flag( DISABLE_WRITES );
    OnBnClickedBtnGetFileEnableStatus();
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnGetFileEnableStatus()
{
    const size_t STRING_SIZE = 30;
    WCHAR local_string[ STRING_SIZE ];

    bool status = mp_C_Log_Writer->Get_Log_File_Write_Enable_Flag( );

   swprintf_s( 
        local_string,
        STRING_SIZE,
        L"%s",
        status == true ? L"Enabled" : L"Disabled" );

    m_file_write_enable_status.SetWindowTextW( local_string );
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnEnableConsoleWrites()
{
    mp_C_Log_Writer->Set_Console_Output_Enable_Flag( ENABLE_WRITES );
    OnBnClickedBtnGetConsoleEnableStatus();
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnDisableConsoleWrites()
{
    mp_C_Log_Writer->Set_Console_Output_Enable_Flag( DISABLE_WRITES );
    OnBnClickedBtnGetConsoleEnableStatus();
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnGetConsoleEnableStatus()
{
    const size_t STRING_SIZE = 30;
    WCHAR local_string[ STRING_SIZE ];

    bool status = mp_C_Log_Writer->Get_Console_Output_Enable_Flag();
    swprintf_s( 
        local_string,
        STRING_SIZE,
        L"%s",
        status == true ? L"Enabled" : L"Disabled" );
    m_console_write_enable_status.SetWindowTextW( local_string );

}


void CTest_C_Log_WriterDlg::OnBnClickedBtnWriteZeroLengthString()
{
    const size_t STRING_SIZE = 30;
    WCHAR local_string[ STRING_SIZE ];
    m_write_zero_length_counter ++;
    bool status = mp_C_Log_Writer->Write_Log_File_Entry( L"" );

    swprintf_s( 
        local_string,
        STRING_SIZE,
        L"%s  %02d",
        status == true ? L"OK    " : L"ERROR ",
        m_write_zero_length_counter  );

    m_zero_length_write_status.SetWindowTextW( local_string );

}


void CTest_C_Log_WriterDlg::OnBnClickedBtnWriteLegalString()
{
   const size_t STRING_SIZE = 30;
   WCHAR local_string[ STRING_SIZE ];
   WCHAR long_string[ MAX_LOG_STRING_LENGTH ] = 
       L"1234567890123456789012345678901234567890123456 050 234567890123456789012345678901234567890123456 100"
       L"1234567890123456789012345678901234567890123456 150 234567890123456789012345678901234567890123456 200"
       L"1234567890123456789012345678901234567890123456 250 234567890123456789012345678901234567890123456 300"
       L"1234567890123456789012345678901234567890123456 350 23456789012345678901234567890123456789012345 399";

    m_write_legal_counter ++;
    bool status = mp_C_Log_Writer->Write_Log_File_Entry( long_string );

    swprintf_s( 
        local_string,
        STRING_SIZE,
        L"%s  %02d",
        status == true ? L"OK    " : L"ERROR ",
        m_write_legal_counter  );

    m_write_legal_string_count.SetWindowTextW( local_string );
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnWriteStringTooLong()
{
   const size_t STRING_SIZE = 30;
   WCHAR local_string[ STRING_SIZE ];
   WCHAR long_string[ 500 ] = 
       L"1234567890123456789012345678901234567890123456 050 234567890123456789012345678901234567890123456 100"
       L"1234567890123456789012345678901234567890123456 150 234567890123456789012345678901234567890123456 200"
       L"1234567890123456789012345678901234567890123456 250 234567890123456789012345678901234567890123456 300"
       L"1234567890123456789012345678901234567890123456 350 234567890123456789012345678901234567890123456 400"
       L"1234567890123456789012345678901234567890123456 450 23456789012345678901234567890123456789012345 499";

       size_t too_long = wcsnlen_s( long_string, 500 );
    m_write_legal_counter ++;
    bool status = mp_C_Log_Writer->Write_Log_File_Entry( long_string );

    swprintf_s( 
        local_string,
        STRING_SIZE,
        L"%s  %02d",
        status == true ? L"OK    " : L"ERROR ",
        m_write_legal_counter  );

    m_write_string_too_long.SetWindowTextW( local_string );
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnSetBadDirectory()
{
    WCHAR dir_string[ MAX_DIRECTORY_NAME_LENGTH ];
   mp_C_Log_Writer->Set_Directory_String( dir_string );
}


void CTest_C_Log_WriterDlg::OnBnClickedBtnSetDirectory()
{
    bool status = false;
    const size_t STRING_SIZE = 30;
    WCHAR local_string[ STRING_SIZE ];

    int user_text_size = m_user_directory_name.GetWindowTextW( m_directory_name, MAX_PATH );
    if( user_text_size > 0 && user_text_size <= MAX_PATH )
    {
        status = mp_C_Log_Writer->Set_Directory_String( m_directory_name );
        m_set_directory_count ++;

        swprintf_s( 
            local_string,
            STRING_SIZE,
            L"%s  %02d",
            status == true ? L"OK    " : L"ERROR ",
            m_set_directory_count  );

        m_directory_set_status.SetWindowTextW( local_string );

    }
}


